﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManagePrepaidAmount : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinAdvanceAmountEntity mSelectedAdvanceAmount;
        #endregion

        #region Constructor
        public frmManagePrepaidAmount()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = " AND ADVANCE_TYPE = 'ADT01'";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND D.DEPARTMENT_ID = '" + department.DepartmentId + "'";
            }
            if (mCanQueryOtherPerson)
            {
                if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
                {
                    MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                    condition += " AND AA.PERSON_ID = '" + person.PersonId + "'";
                }
            }
            else
            {
                condition += " AND AA.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND AA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpAdvanceDate.EditValue!=null)
            {
                condition += " AND AA.ADVANCE_DATE >= CONVERT(DATETIME,'" + dtpAdvanceDate.EditValue.ToString() + " 00:00:00')";
            }
            if (dtpAdvanceDateTo.EditValue != null)
            {
                condition += " AND AA.ADVANCE_DATE <= CONVERT(DATETIME,'" + dtpAdvanceDateTo.EditValue.ToString() + " 23:59:59')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ADVANCE_DATE DESC";
            DataTable dt = FinAdvanceAmountBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnAdvance.Enabled = false;
            if (mSelectedAdvanceAmount == null)
            {
                return;
            }
            if (mSelectedAdvanceAmount.Status == "AMS01" || mSelectedAdvanceAmount.Status == "AMS03")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedAdvanceAmount.Status == "AMS02")
            {
                btnAdvance.Enabled = true;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.财务);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "AMS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "AMS", true);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAdvanceAmount = new FinAdvanceAmountEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedAdvanceAmount == null)
            {
                MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPrepaidAmount frm = new frmMaintainPrepaidAmount(mSelectedAdvanceAmount);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedAdvanceAmount == null)
            {
                MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中预支申请？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FinAdvanceAmountBs.DeleteFinAdvanceAmount(mSelectedAdvanceAmount);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedAdvanceAmount == null)
            {
                MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPrepaidAmount frm = new frmMaintainPrepaidAmount(mSelectedAdvanceAmount);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedAdvanceAmount == null)
            {
                MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPrepaidAmount frm = new frmMaintainPrepaidAmount(mSelectedAdvanceAmount);
            frm.ThisActionType = ActionType.Approve;
            frm.ThisApproveType = ApproveType.CEO;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainPrepaidAmount frm = new frmMaintainPrepaidAmount();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Print()
        {
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Tag = frm.SelectedDepartment;
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
            }
            else
            {
                txtDepartment.Tag = null;
                txtDepartment.Text = "";
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }

        private void btnAdvance_Click(object sender, EventArgs e)
        {
            //if (mSelectedAdvanceAmount == null)
            //{
            //    MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //DialogResult dr = MessageBox.Show("是否确定支付预支金额？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (dr == DialogResult.No)
            //{
            //    return;
            //}
            //try
            //{
            //    mSelectedAdvanceAmount.Status = "AMS04";
            //    bool successful = FinAdvanceAmountBs.ApproveAdvanceAmount(mSelectedAdvanceAmount);
            //    if (successful)
            //    {
            //        MessageBox.Show("支付成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        BindDataGrid();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}

            if (mSelectedAdvanceAmount == null)
            {
                MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPrepaidAmount frm = new frmMaintainPrepaidAmount(mSelectedAdvanceAmount);
            frm.ThisActionType = ActionType.Approve;
            frm.ThisApproveType = ApproveType.FIN;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnAdvanceHistory_Click(object sender, EventArgs e)
        {
            if (mSelectedAdvanceAmount == null)
            {
                MessageBox.Show("请选择一个预支申请！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectedAdvanceAmount.PersonId);
            if (person != null)
            {
                frmMaintainPrepaidAmountHistory frm = new frmMaintainPrepaidAmountHistory(person);
                frm.ViewOnly = true;
                frm.ShowDialog();
            }
        }
        #endregion

    }
}
